{% extends 'extends/nav_back_all_.html' %}

{% block main %}
<div class="content-wrapper">
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1>内容审核</h1>
                </div>
            </div>
        </div>
    </section>

    <section class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-12">
                    <div class="card">

                        <div class="card-body">
                            <table id="auditTable" class="table table-bordered table-hover">
                                <thead>
                                    <tr>
                                        <th>帖子</th>
                                        <th>类别</th>
                                        <th>日期</th>
                                        <th>意见</th>
                                    </tr>
                                </thead>
                                <tbody id="body">
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>

{% endblock %}

{% block script %}
<script>
    onUrlsReady(urls=>{

        async function PassEvent(pid) {
            await Fetch(urls.info.admincheck, 'post', {pid: pid}, res => {
                Flash(res.msg)
            })
        }

        async function UnpassEvent(input_id, pid) {
            await Fetch(urls.info.admincheck, 'delete', {pid: pid,text:Element_('input_id').value}, res => {
                Flash(res.msg)
            })
        }

        Fetch(urls.info.admincheck, 'get', {}, res => {
            if (res.status === 'success') {
                res.data.forEach(blog => {
                    const post_url = urls.page.front.blogdetail.replace('0', blog.pid);
                    const row = table_item(
                        `<a href="${post_url}">${blog.pid}</a>`,
                        `${blog.sort}`,
                        `${blog.time}`,
                        `<input id='reject-input-${blog.pid}' type="text" class="reject-reason-input" placeholder="请输入拒绝理由...">
                         <button id='reject-but-${blog.pid}' class="btn btn-danger reject-btn">拒绝</button>
                         <button id='pass-but-${blog.pid}' class="btn btn-success approve-btn">通过</button>`
                    );

                    // 添加到表格
                    Element_('body').insertAdjacentHTML('beforeend', row);


                    // 为通过按钮绑定事件
                    Event_(`pass-but-${blog.pid}`, 'click', () => {
                        Fetch(urls.info.admincheck, 'post', {pid: blog.pid}, async res => {
                            await Flash(res.msg);
                            Redirect()
                        });
                    });

                    // 为拒绝按钮绑定事件
                    Event_(`reject-but-${blog.pid}`, 'click', async () => {
                        const reasonInput = document.getElementById(`reject-input-${blog.pid}`);
                        const reason = reasonInput.value.trim();

                        if (!reason) {
                            await Flash('请填写拒绝理由');
                            return;
                        }

                        Fetch(urls.info.admincheck, 'delete', {
                            pid: blog.pid,
                            text: reason
                        }, async res => {
                            await Flash(res.msg);
                            Redirect()
                        });
                    });
                });
            }
        });
    })
</script>
{% endblock %}